package com.itheima.socket.tcp.demo06;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 练习题1： 客户端发送数据，服务器接收数据并给出反馈；
 */
public class ServerDemo01 {

    public static void main(String[] args) throws IOException {

        // 1. 创建ServerSocket对象
        ServerSocket serverSocket = new ServerSocket(8080);

        // 2. 监听客户端
        Socket socket = serverSocket.accept();

        // 3. 获取输入流对象
        InputStream inputStream = socket.getInputStream();

        // 4. 读取数据
        byte[] bytes = new byte[1024] ;
        int len = inputStream.read(bytes);
        System.out.println(new String(bytes , 0  , len , "UTF-8"));

        // 5. 获取输出流对象
        OutputStream outputStream = socket.getOutputStream();

        // 6. 写数据
        outputStream.write("ok,收到了".getBytes("UTF-8"));

        // 7. 释放资源
        socket.close();
        serverSocket.close();

    }

}
